package com.dingreading.cloud.app.controller;


import com.dingreading.cloud.app.entity.Books;
import com.dingreading.cloud.app.entity.Corporations;
import com.dingreading.cloud.app.entity.StoreBooks;
import com.dingreading.cloud.app.entity.Stores;
import com.dingreading.cloud.app.service.BooksService;
import com.dingreading.cloud.app.service.CorporationsService;
import com.dingreading.cloud.app.service.StoreBooksService;
import com.dingreading.cloud.app.service.StoresService;
import com.dingreading.cloud.app.util.BooksUtil;
import com.dingreading.cloud.app.util.UidUtil;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.dto.BookSimpleDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/app/books")
@Api(tags = "App端-书籍功能")
public class AppBookController {

    @Resource
    private CorporationsService corporationsService;
    @Resource
    private StoresService storesService;
    @Resource
    private BooksService booksService;
    @Resource
    private StoreBooksService storeBooksService;

    @ApiOperation(value = "阿里云isbn号查询书籍")
    @PostMapping(value = "/aliyunFindBook", produces = "application/json; charset=UTF-8")
    public R<Object> aliyunFindBook(
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid", required = false) String storeUid,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "isbn号") @RequestParam(value = "isbn") String isbn,
            @ApiParam(value = "书名") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "标签id") @RequestParam(value = "bookLabelId", required = false) Long bookLabelId,
            @ApiParam(value = "标签名") @RequestParam(value = "bookLabelName", required = false) String bookLabelName
    ) throws Exception {
        /*Object staffObj = UidUtil.getStaff(staffUid);
        if (staffObj instanceof String) return R.fail(staffObj.toString());
        Staff staff = (Staff) staffObj;

        if (isbn.length() != 13) return R.fail("isbn号异常");

        String bookNum = null;
        Books books = null;
        AliyunBookDto aliyunBookDto = AliyunBookUtil.findBook(isbn);
        if (aliyunBookDto == null) {
            return R.fail("没有查找到书籍");
        } else {
            String status = aliyunBookDto.getStatus();
            if (status == null || !status.equals("0")) {
                return R.fail(AliyunBookUtil.getMessage(status));
            }
            AliyunBookDto.ResultDTO resultDto = aliyunBookDto.getResult();
            if (resultDto == null) {
                return R.fail("书籍详情null");
            }
            Date datetime = DateUtil.getNowDatetime();

            // 获取书号
            Books maxBooks = booksService.getMaxBookNum();
            try {
                bookNum = BooksUtil.getBookNum(maxBooks);
            } catch (Exception e) {
                throw new DescribeException(500, "获取获取最新书号错误");
            }
//                Long maxId = booksService.getMaxId();
//                bookNum = BooksUtil.getBookNum(maxId, 1);
            //新增书籍
            books = new Books();
            books.setBookNum(bookNum);
            if (StringUtils.isNotBlank(name)) {
                books.setBookName(name);
            } else {
                books.setBookName(resultDto.getTitle());
            }
            if (bookLabelId != null && bookLabelId > 0) {
                books.setBookLabelId(bookLabelId);
            }
            if (StringUtils.isNotBlank(bookLabelName)) {
                books.setBookLabelName(bookLabelName);
            }
            books.setAuthor(resultDto.getAuthor());
            books.setImg(resultDto.getPic());

            double price = AliyunBookUtil.getPrice(resultDto.getPrice());
            books.setUnitPrice(price);

            books.setPress(resultDto.getPublisher());
            books.setSynopsis(resultDto.getSummary());
            books.setIsbn(isbn);
            books.setSource(2);
            books.setSourceStoreId(staff.getStoreId());
            books.setStatus(1);
            books.setAddTime(datetime);
            books = booksService.saveOrUpdate(books);

            if (books == null || books.getId() == null || books.getId() <= 0) {
                return R.fail("添加书籍失败");
            }

            // 书籍同步到门店
            StoreBooks storeBooks = BooksUtil.newStoreBooks(books, staff.getStoreId(), datetime, 2);
            storeBooksService.saveOrUpdate(storeBooks);
        }

        BookSimpleDto bookSimpleDto = BooksUtil.newBookSimpleDto(books.getId(), null, 1, 1,
                books.getIsbn(), books.getBookNum(), books.getBookName(), books.getAuthor(), books.getBookLabelId(),
                books.getBookLabelName(), books.getBookType());
        return R.ok(bookSimpleDto);*/
        return R.fail("功能已暂停");
    }

    @ApiOperation(value = "isbn号/书号查询书籍")
    @PostMapping(value = "/getBook", produces = "application/json; charset=UTF-8")
    public R<Object> getBook(
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "isbn号/书名/书号") @RequestParam(value = "isbn") String isbn
    ) {
//        Object staffObj = UidUtil.getStaff(staffUid);
//        if (staffObj instanceof String) return R.fail(staffObj.toString());
//        Staff staff = (Staff) staffObj;
//
//        List<BookSimpleDto> bookSimpleList = new ArrayList<>();
//        List<StoreBooks> storeBooksList = storeBooksService.getByStoreIdAndIsbnOrBookNumOrName(staff.getStoreId(), isbn);
//        if (CollectionUtils.isNotEmpty(storeBooksList)) {
//            bookSimpleList = BooksUtil.getBookSimpleDtoByStoreBook(storeBooksList);
//            return R.ok(bookSimpleList);
//        }
//
//        List<Books> bookList = booksService.getByIsbnOrBookNumOrName(isbn);
//        if (CollectionUtils.isNotEmpty(bookList)) {
//            bookSimpleList = BooksUtil.getBookSimpleDto(bookList);
//            return R.ok(bookSimpleList);
//        }
//        return R.ok(bookSimpleList);

        return R.fail("功能已暂停，请升级App版本");
    }

    @ApiOperation(value = "isbn号/书号查询书籍")
    @PostMapping(value = "/v2/getBook", produces = "application/json; charset=UTF-8")
    public R<Object> getBookV2(
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "isbn号/书名/书号") @RequestParam(value = "isbn") String isbn
    ) {
        Stores stores = storesService.getByUid(storeUid);
        if (null == stores) return R.fail("门店获取失败");
        if (!stores.getEnabled().equals(1)) return R.fail("门店已被禁用");

        Corporations corporations = corporationsService.getByUid(stores.getCorpUid());
        String judgeCorp = UidUtil.judgeCorp(corporations);
        if (StringUtils.isNotBlank(judgeCorp))
            return R.fail(judgeCorp);

        List<BookSimpleDto> bookSimpleList = new ArrayList<>();
        List<StoreBooks> storeBooksList = storeBooksService.getByStoreIdAndIsbnOrBookNumOrName(stores.getId(), isbn);
        if (CollectionUtils.isNotEmpty(storeBooksList)) {
            bookSimpleList = BooksUtil.getBookSimpleDtoByStoreBook(storeBooksList);
            return R.ok(bookSimpleList);
        }

        List<Books> bookList = booksService.getByIsbnOrBookNumOrName(isbn);
        if (CollectionUtils.isNotEmpty(bookList)) {
            bookSimpleList = BooksUtil.getBookSimpleDto(bookList);
            return R.ok(bookSimpleList);
        }
        return R.ok(bookSimpleList);
    }

    @ApiOperation(value = "修改门店书籍的分类")
    @PostMapping(value = "/editBookLabel", produces = "application/json; charset=UTF-8")
    public R<Object> editBookLabel(
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "门店书籍id") @RequestParam(value = "storeBookId") Long storeBookId,
            @ApiParam(value = "分类id") @RequestParam(value = "labelId") Long labelId
    ) {
//        Object staffObj = UidUtil.getStaff(staffUid);
//        if (staffObj instanceof String) return R.fail(staffObj.toString());
//        Staff staff = (Staff) staffObj;
//
//        if (null == storeBookId || storeBookId <= 0) return R.fail("不是门店书籍，不能操作");
//
//        BookLabel bookLabel = bookLabelService.get(labelId);
//        if (bookLabel == null) return R.fail("分类获取失败");
//
//        StoreBooks storeBooks = storeBooksService.get(storeBookId);
//        if (storeBooks == null) return R.fail("门店书籍获取失败");
//
//        // 更新门店书籍的分类
//        long l = storeBooksService.updateLabel(staff.getStoreId(), storeBookId, labelId, bookLabel.getLabelName());
//        if (l > 0) {
//            return R.ok();
//        } else {
//            return R.fail("修改失败");
//        }
        return R.fail("功能已暂停");
    }

    @ApiOperation(value = "删除门店书籍")
    @PostMapping(value = "/dropStoreBook", produces = "application/json; charset=UTF-8")
    public R<Object> dropStoreBook(
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "门店书籍id") @RequestParam(value = "storeBookId") Long storeBookId
    ) {
//        Object staffObj = UidUtil.getStaff(staffUid);
//        if (staffObj instanceof String) return R.fail(staffObj.toString());
//        Staff staff = (Staff) staffObj;
//
//        if (null == storeBookId || storeBookId <= 0) return R.fail("不是门店书籍，不能操作");
//
//        long l = storeBooksService.deleteByStoreIdAndId(staff.getStoreId(), storeBookId);
//        if (l > 0) {
//            return R.ok();
//        } else {
//            return R.fail("删除失败");
//        }
        return R.fail("功能已暂停");
    }

    @ApiOperation(value = "获取书籍分类")
    @PostMapping(value = "/getBookLabels", produces = "application/json; charset=UTF-8")
    public R<Object> getBookLabels(
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid
    ) {
//        Object staffObj = UidUtil.getStaff(staffUid);
//        if (staffObj instanceof String) return R.fail(staffObj.toString());
//        Staff staff = (Staff) staffObj;
//
//        List<BookLabel> bookLabels = bookLabelService.getAll();
//
//        return R.ok(bookLabels);
        return R.fail("功能已暂停");
    }
}
